package com.wbp.cms.client.result;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 各业务模块使用错误码区间： 1 --- 10000 : 公共 10001 --- 20000:  
 * 
 */
public class BaseResultCode implements ResultCode, Serializable {
  private static final long serialVersionUID = 57979977446201966L;

  public static final int ERROR_CODE_SUCCESS = 200;
  public static final int ERROR_CODE_NETWORK_ERROR = 400;
  public static final int ERROR_CODE_SYS_ERROR = 500;
  public static final int ERROR_CODE_DB_ERROR = 501;

  public static final int ERROR_CODE_INVALID_PARAMETER = 601;
  public static final int ERROR_CODE_INVALID_USER = 602;
  public static final int ERROR_CODE_IS_NOT_NEW_USER = 603;

  public static final int ERROR_CODE_NOT_REAL_NAME_AUTH = 700;  
  public static final int ERROR_CODE_UNBINDED_ALIPAY = 701;

  public static final BaseResultCode SUCCESS = new BaseResultCode(ERROR_CODE_SUCCESS, "成功",
      INFO_LEVEL);
  public static final BaseResultCode NETWORK_ERROR = new BaseResultCode(ERROR_CODE_NETWORK_ERROR,
      "网络错误", ERROR_LEVEL);
  public static final BaseResultCode SYS_ERROR = new BaseResultCode(ERROR_CODE_SYS_ERROR, "系统错误",
      ERROR_LEVEL);
  public static final BaseResultCode DB_ERROR = new BaseResultCode(ERROR_CODE_DB_ERROR, "DB错误",
      ERROR_LEVEL);
  public static final BaseResultCode INVALID_PARAMETER = new BaseResultCode(
      ERROR_CODE_INVALID_PARAMETER, "无效参数", WARN_LEVEL);
  public static final BaseResultCode INVALID_USER = new BaseResultCode(ERROR_CODE_INVALID_USER,
      "无效用户", WARN_LEVEL);
 
 
  public static final BaseResultCode CHECK_CODE_EXPIRED = new BaseResultCode(701, "验证码已经过期");
  public static final BaseResultCode CHECK_CODE_FAILED = new BaseResultCode(702, "验证码错误");
 

  public static final BaseResultCode ILLEGAL_CHARATERS = new BaseResultCode(1001, "入参包含非法字符");

  /**
   * 用户注册，登录时的错误 码
   */
  public static final BaseResultCode USER_USERNAME_NOT_EMPTY = new BaseResultCode(10001, "用户名不能为空");
  public static final BaseResultCode USER_USERNAME_MUST_BE_MOBILE_NUMBER =new BaseResultCode(10002, "用户名必须是手机号码");
  public static final BaseResultCode USER_MOBILE_NUMBER_IS_INVALID = new BaseResultCode(10003, "手机号码格式不正确");
  public static final BaseResultCode USER_PASSWORD_NOT_EMPTY  = new BaseResultCode(10004, "密码不能为空");
  public static final BaseResultCode USER_PASSWORD_LENGTH_IN6AND8 = new BaseResultCode(10005, "密码要为6~8位之间");
  public static final BaseResultCode USER_USERNAME_MUST_BE_EMAIL = new BaseResultCode(10005, "用户名必须是Email");
  public static final BaseResultCode USER_USERNAME_EMAIL_IS_INVALID = new BaseResultCode(10006,"Email格式不对");
  public static final BaseResultCode USER_USERNAME_ERROR = new BaseResultCode(10007, "用户名错误");
  public static final BaseResultCode USER_PASSOWRD_ERROR = new BaseResultCode(10008, "密码错误");

  /**
   * 客户错误 码
   */
  public static final BaseResultCode CUSTOMER_ADD_CUSTOMER_ERROR = new BaseResultCode(11001, "新增客户失败");
  public static final BaseResultCode CUSTOMER_MODIFY_CUSTOMER_ERROR = new BaseResultCode(11002, "修改客户失败");
  public static final BaseResultCode CUSTOMER_QUERY_CUSTOMER_ERROR = new BaseResultCode(11003, "查询客户列表失败");
  
  /**
   * 用户错误码
   */
  public static final BaseResultCode USER_ADD_USER_ERROR = new BaseResultCode(12001, "新增用户失败");
  public static final BaseResultCode USER_MODIFY_USER_ERROR = new BaseResultCode(12002, "修改用户失败");
  public static final BaseResultCode USER_QUERY_USER_ERROR = new BaseResultCode(12003, "查询用户列表失败");
  
  /**
   * 预约项目错误码
   */
  public static final BaseResultCode PROJECT_QUERY_NOT_FUND = new BaseResultCode(13001, "没有查到预约项目信息");
  public static final BaseResultCode PROJECT_ADD_PROJECT_ERROR = new BaseResultCode(13002, "新增预约项目失败");
  public static final BaseResultCode PROJECT_MODIFY_PROJECT_ERROR = new BaseResultCode(13003, "修改预约项目失败");
  public static final BaseResultCode PROJECT_QUERY_PROJECT_ERROR = new BaseResultCode(13004, "查询预约项目列表失败");
  
  /**
   * 字典值
   */
  public static final BaseResultCode DICTIONARY_ADD_DICTIONARY_ERROR = new BaseResultCode(14001, "新增字典值类型失败");
  public static final BaseResultCode DICTIONARY_MODIFY_DICTIONARY_ERROR = new BaseResultCode(14001, "修改字典值类型失败");
  
  
  public static final BaseResultCode CONTRACT_QUERY_NOT_FUND = new BaseResultCode(15001, "没有查到合同信息");
  
  
  public static final BaseResultCode PRODUCT_ADD_PRODUCT_ERROR = new BaseResultCode(16001, "新增产品失败");
  public static final BaseResultCode PRODUCT_MODIFY_PRODUCT_ERROR = new BaseResultCode(16002, "修改产品失败");
  
  
  
  private int errCode; // 错误码
  private String errName; // 错误名，如 SYS_ERROR，可用于告警的识别
  private String errMsg; // 错误信息
  private String errTip; // 错误提示文案
  private int errLevel; // 错误级别


  protected BaseResultCode(String errName, int errCode, String errMsg, String errTip, int errLevel) {
    this.errCode = errCode;
    this.errName = errName;
    this.errMsg = errMsg;
    this.errTip = errTip;
    this.errLevel = errLevel;
  }

  protected BaseResultCode(int errCode, String errMsg, String errTip, int errLevel) {
    this.errCode = errCode;
    this.errMsg = errMsg;
    this.errTip = errTip;
    this.errLevel = errLevel;
  }

  protected BaseResultCode(String errName, int errCode, String errMsg, int errLevel) {
    this.errName = errName;
    this.errCode = errCode;
    this.errMsg = errMsg;
    this.errTip = errMsg;
    this.errLevel = errLevel;
  }

  protected BaseResultCode(int errCode, String errMsg, int errLevel) {
    this.errCode = errCode;
    this.errMsg = errMsg;
    this.errTip = errMsg;
    this.errLevel = errLevel;
  }


  protected BaseResultCode(int errCode, String errMsg) {
    this.errCode = errCode;
    this.errMsg = errMsg;
    this.errTip = errMsg;
    this.errLevel = ResultCode.ERROR_LEVEL;
  }

  public int getErrCode() {
    return this.errCode;
  }

  public String getErrMsg() {
    return this.errMsg;
  }

  public String getErrTip() {
    return this.errTip;
  }

  public String getErrName() {
    return this.errName;
  }

  public int getErrLevel() {
    return this.errLevel;
  }

  @Override
  public ResultCode setErrCode(int errCode) {
    this.errCode = errCode;
    return this;
  }

  @Override
  public ResultCode setErrName(String errName) {
    this.errName = errName;
    return this;
  }

  @Override
  public ResultCode setErrMsg(String errMsg) {
    this.errMsg = errMsg;
    return this;
  }

  @Override
  public ResultCode setErrTip(String errTip) {
    this.errTip = errTip;
    return this;
  }

  @Override
  public ResultCode setErrLevel(int errLevel) {
    this.errLevel = errLevel;
    return this;
  }

  public static ResultCode buildResultCode(final String errName, final int errCode,
      final String errMsg, final String errTip, final int errLevel) {
    return new BaseResultCode(errName, errCode, errMsg, errTip, errLevel);
  }

  public static ResultCode buildResultCode(final String errName, final int errCode,
      final String errMsg, final int errLevel) {
    return buildResultCode(errName, errCode, errMsg, null, errLevel);
  }

  public static ResultCode buildResultCode(final int errCode, final String errMsg,
      final int errLevel) {
    return buildResultCode(null, errCode, errMsg, null, errLevel);
  }

  public static ResultCode buildResultCode(final String errName, final int errCode,
      final String errMsg) {
    return buildResultCode(errName, errCode, errMsg, null, SYS_ERROR.getErrLevel());
  }

  public static ResultCode buildResultCode(final int errCode, final String errMsg) {
    return buildResultCode(null, errCode, errMsg, null, SYS_ERROR.getErrLevel());
  }

  public ResultCode appendErrMsg(String msg) {
    this.errMsg = this.errMsg + ", " + msg;
    return this;
  }

  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }
}
